package net.bbtang.website.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bbtang.website.pojo.CarouselImagesDTO;
import net.bbtang.website.service.ImageCarouselService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/carousel")
public class ImageCarouselApiController {
	@Autowired
	ImageCarouselService imageCarouselService;
	
	private static final Logger logger = Logger.getLogger(ImageCarouselApiController.class); 
	
	/**获取轮播图信息
	 @api {post} /carousel/getImageCarouselInfo 获取轮播图信息
	 
	 @apiParam {int} useUniversity 学校id 必填
	 @apiParam {int} imageState 图片状态（0:未开通学校的轮播图 1：已经开通的轮播图 -1：禁用） 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"useUniversity":1,
		"imageState":1,
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		  "number": "2000",
		  "flag": true,
		  "data": [
		    {
		      "id": 2,    图片id
		      "imageTitle": "轮播2",  轮播图id
		      "imageUrl": "www.taobao.com",  轮播图地址
		      "imageBegintime": 1488726715000,  展示开始时间
		      "imageEndtime": 1493565117000,  展示结束时间
			  "isJump": 1,  是否跳转（1：是 0：否）
		      "jumpType": 1,  跳转类型（0：跳转H5 1：跳转站内页）
		      "h5Url": "www.qq.com",  H5地址（jumpType=0时有此值）
		      " useCity ": 2,  作用城市（预留
		      "useUniversity": 1,  作用学校
		      "operator": "李四",  操作人
		      "activityIntroduce": "轮播图2的描述",  轮播图介绍
		      "isTop": 0,  是否置顶（1：是，0：否）后台已做置顶操作，忽略此字段
		      "imageState": 1,  轮播图状态（0:未开通学校的轮播图 1：已经开通的轮播图 -1：禁用）
		      "createTime": 1492096780000,  创建时间
		      " updateTime ": 1492096782000  更新时间
		    },
		    {
		      "id": 1,
		      "imageTitle": "轮播1",
		      "imageUrl": "www.baidu.com",
		      "imageBegintime": 1485961333000,
		      "imageEndtime": 1492268537000,
		      "isJump": 1,
		      "jumpType": 0,
		      "jumpPageApp": 1,
		      "useCity": 1,
		      "useUniversity": 1,
		      "operator": "张三",
		      "activityIntroduce": "轮播图1的描述",
		      "isTop": 0,
		      "imageState": 1,
		      "createTime": 1492095792000,
		      "updateTime": 1492095795000
		    }
		  ]
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取账户积分信息异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getImageCarouselInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object getImageCarouselInfo(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer useUniversity = jb.getInt("useUniversity");//投放范围
			Integer imageState = jb.getInt("imageState");//状态
		    Map<String,Object> map = new HashMap<String,Object>();
		    map.put("useUniversity", useUniversity);
		    map.put("imageState", imageState);
			List<CarouselImagesDTO> imageCarouselList  = imageCarouselService.getImageCarouselInfo(map);
			sc.setFlag(true);
			sc.setData(imageCarouselList);
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取轮播图信息异常");
		}
		
		return sc;
	
	}
	
	
	/**获取展示图片信息
	 @api {get} /carousel/getShowImg 获取展示图片信息
	 

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	

	@apiSuccessExample {json} 请求成功结果Demo:
	{
    "number": "2000",
    "flag": true,
    "data": [
        {
            "id": 3,   id
            "showImgUrl": "/mobile_img/img1.jpg",   图片地址(显示图片为  服务器域名或ip+bbtang项目名+showImgUrl字段拼成的地址)
            "version": 2,     版本(每次取最高版本，移动端可忽略此字段)
            "showLevel": 1,   显示顺序控制(按此字段升序显示，移动端可忽略此字段)
            "state": 0,       状态(0：正常  1：删除，移动端可忽略此字段)
            "showTime": 1494252029000   时间(移动端可忽略此字段)
        },
        {
            "id": 2,
            "showImgUrl": "/mobile_img/img2.jpg",
            "version": 2,
            "showLevel": 2,
            "state": 0,
            "showTime": 1494252019000
        },
        {
            "id": 1,
            "showImgUrl": "/mobile_img/img2.jpg",
            "version": 2,
            "showLevel": 3,
            "state": 0,
            "showTime": 1494252003000
        }
    ]
}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取展示图片异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getShowImg", method = RequestMethod.GET)
	@ResponseBody
	public Object getShowImg(HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			sc.setFlag(true);
			sc.setData(imageCarouselService.getShowImageUrl());
			
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取展示图片异常");
		}
		
		return sc;
	
	}

	
	
	
}
